#include <graphics.h>
#include <conio.h>
#include <Windows.h>
#include <string.h>

#pragma comment(lib,"Winmm.lib")
//定义画面大小
#define HIGH 640
#define WIDTH 640

IMAGE img_bk;				//背景
IMAGE img_stu1, img_stu2;	//学生

struct background			//背景坐标
{
	int x;
	int y;
};
background bk;

struct student
{
	int x;
	int y;					//坐标
	int status;				//朝向0表示下，1表示上，2表示左，3表示右
};
student stu;

int down , up, left, right;	//移动时计数

void startup()
{
	initgraph(HIGH, WIDTH);
	loadimage(&img_bk, ".\\background.jpg");
	loadimage(&img_stu1, ".\\学生2.jpg");
	loadimage(&img_stu2, ".\\学生1.jpg");

	stu.x = 320;
	stu.y = 320;
	stu.status = 0;

	bk.x =0;
	bk.y =0;

	down = 0;
	up = 0;
	left = 0;
	right = 0;

	BeginBatchDraw();
}

void show()
{
	putimage(bk.x, bk.y, &img_bk);

	if (stu.status == 0)
	{
		putimage(stu.x, stu.y, 32, 48, &img_stu1, 32 * down, 0, NOTSRCERASE);
		putimage(stu.x, stu.y, 32, 48, &img_stu2, 32 * down, 0, SRCINVERT);
	}
	if (stu.status == 1)
	{
		putimage(stu.x, stu.y, 32, 48, &img_stu1, 32 * up, 144, NOTSRCERASE);
		putimage(stu.x, stu.y, 32, 48, &img_stu2, 32 * up, 144, SRCINVERT);
	}
	if (stu.status == 2)
	{
		putimage(stu.x, stu.y, 32, 48, &img_stu1, 32 * left, 48, NOTSRCERASE);
		putimage(stu.x, stu.y, 32, 48, &img_stu2, 32 * left, 48, SRCINVERT);
	}
	if (stu.status == 3)
	{
		putimage(stu.x, stu.y, 32, 48, &img_stu1, 32 * right, 96, NOTSRCERASE);
		putimage(stu.x, stu.y, 32, 48, &img_stu2, 32 * right, 96, SRCINVERT);
	}

}

void updateWithoutInput()
{

}

void updateWithInput()
{
	char input;
	if (kbhit())
	{
		input = getch();
		if (input == 's')
		{
			if (bk.y >= -1079 && bk.y <= 10 && stu.y >= 320)
				bk.y = bk.y - 10;
			stu.status = 0;
			up = 0;
			left = 0;
			right = 0;
			clearrectangle(stu.x, stu.y, stu.x + 32, stu.y + 48);
			if (bk.y >= -10 && bk.y <= 0 && stu.y <= 320 && stu.y >= 0)
				stu.y = stu.y + 10;
			else if (bk.y >= -1089 && bk.y <= -1079 && stu.y >= 320 && stu.y <= 580)
				stu.y = stu.y + 10;
			down = down + 1;
			putimage(stu.x, stu.y, 32, 48, &img_stu1, 32 * down, 0, NOTSRCERASE);
			putimage(stu.x, stu.y, 32, 48, &img_stu2, 32 * down, 0, SRCINVERT);
		}
		if (input == 'w')
		{
			if (bk.y >= -1089 && bk.y <= -10 && stu.y <= 330)
				bk.y = bk.y + 10;
			stu.status = 1;
			down = 0;
			left = 0;
			right = 0;
			clearrectangle(stu.x, stu.y, stu.x + 32, stu.y + 48);
			if (bk.y >= -10 && bk.y <= 0 && stu.y <= 330 && stu.y >= 10)
				stu.y = stu.y - 10;
			else if (bk.y >= -1089 && bk.y <= -1079 && stu.y >= 320 && stu.y <= 620)
				stu.y = stu.y - 10;
			up = up + 1;
			putimage(stu.x, stu.y, 32, 48, &img_stu1, 32 * up, 144, NOTSRCERASE);
			putimage(stu.x, stu.y, 32, 48, &img_stu2, 32 * up, 144, SRCINVERT);
		}
		if (input == 'a')
		{
			if (bk.x >= -2460 && bk.x <= -10 && stu.x <= 330)
				bk.x = bk.x + 10;
			stu.status = 2;
			down = 0;
			up = 0;
			right = 0;
			clearrectangle(stu.x, stu.y, stu.x + 32, stu.y + 48);
			if (bk.x >= -10 && bk.x <= 0 && stu.x <= 330 && stu.x >= 10)
				stu.x = stu.x - 10;
			else if (bk.x <= -2450 && bk.x >= -2460 && stu.x >= 310 && stu.x <= 620)
				stu.x = stu.x - 10;
			left = left + 1;
			putimage(stu.x, stu.y, 32, 48, &img_stu1, 32 * left, 48, NOTSRCERASE);
			putimage(stu.x, stu.y, 32, 48, &img_stu2, 32 * left, 48, SRCINVERT);
		}
		if (input == 'd')
		{
			if (bk.x >= -2450 && bk.x <= 10 && stu.x >= 320)
				bk.x = bk.x - 10;
			stu.status = 3;
			down = 0;
			up = 0;
			left = 0;
			clearrectangle(stu.x, stu.y, stu.x + 32, stu.y + 48);
			if (bk.x <= -2450 && bk.x >= -2460 && stu.x >= 310 && stu.x <= 600)
				stu.x = stu.x + 10;
			else if (bk.x >= -10 && bk.x <= 0 && stu.x <= 320 && stu.x >= 0)
				stu.x = stu.x + 10;
			right = right + 1;
			putimage(stu.x, stu.y, 32, 48, &img_stu1, 32 * right, 96, NOTSRCERASE);
			putimage(stu.x, stu.y, 32, 48, &img_stu2, 32 * right, 96, SRCINVERT);
		}
	}

	FlushBatchDraw();
	if (down == 4)
		down = 0;
	if (up == 4)
		up = 0;
	if (left == 4)
		left = 0;
	if (right == 4)
		right = 0;
}

void gameover()
{
	EndBatchDraw();
	getch();
	closegraph();
}
int main()
{
	startup();  // 数据初始化	
	while (1)  //  游戏循环执行
	{
		show();  // 显示新画面
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();     // 与用户输入有关的更新
	}
	gameover();     // 游戏结束、后续处理
	return 0;
}